# -*- coding: utf-8 -*-
from selenium import webdriver                         # 失败（无法点击搜索按钮跳转至新的页面）
import time


if __name__ == "__main__":

    base_url = 'https://uland.taobao.com/sem/tbsearch?refpid=mm_26632258_3504122_32538762&clk1=e4453a161fce889e813ff3235&keyword=%E7%AC%94%E8%AE%B0%E6%9C%AC%E7%94%B5%E8%84%91&page=0'

    text = input("请输入您想要爬取的电脑相关信息:")
    t = int(input("请输入您想要爬取的页数:"))


    driver = webdriver.Firefox()
    driver.get(base_url)
    driver.find_element_by_id('q').clear()
    driver.find_element_by_id('q').send_keys(text)
    driver.save_screenshot('lk.png')
    # time.sleep(2)
    driver.find_element_by_css_selector('#J_searchbox').click()                                 #    *********************
    # driver.find_element_by_link_text("搜索").click()
    # driver.save_screenshot('lk1.png')
    # time.sleep(2)
    now_handles = driver.current_window_handle
    print(now_handles)  # 输出当前窗口句柄
    handles = (driver.window_handles)  # 获取当前窗口句柄集合（列表类型）
    print(handles)  # 输出句柄集合




    for i in range(t):
        driver.find_element_by_xpath('//*[@id="J_waterfallPagination"]/div/div/a[4]').click()
        print('switch to ', handles[1])
        driver.switch_to_window(handles[1])
        # html = driver.page_source
        # spider(html, choose)

    driver.close()  # 关闭当前窗口
    driver.switch_to_window(handles[0])  # 切换回原窗口

    driver.quit()




